//index.js
var pageSize = 10;
var postType = 'sitehome';
var newsType = 'recommended';

//获取应用实例
var app = getApp()
Page({
    data: {
        tabs: ["博文", "精华区", "推荐新闻", "热门新闻", "本周新闻"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,

        siteHomeIndex: 1,
        pickedIndex: 1,
        recommendedIndex: 1,
        hotIndex: 1,
        hotWeekIndex: 1,

        //首页博文列表
        blog_articles: [],
        //精华区博文列表
        blog_pickeds: [],
        //推荐新闻
        news_recommendeds: [],
        //热门新闻
        news_hots: [],
        //本周热门新闻
        news_hots_week: []
    },
    onLoad: function () {
        //默认加载首页文章列表
        this.loadMoreBlogPosts(this, this.data.siteHomeIndex, postType);
    },

    onPullDownRefresh: function () {
        console.log('--------下拉刷新-------')
        wx.showNavigationBarLoading()
        this.changeTab(this.data.activeIndex, 'pull');
    },

    onReachBottom: function () {
        console.log('--------上拉加载-------')
        this.changeTab(this.data.activeIndex, 'reach');
    },

    //tabs
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });

        this.changeTab(this.data.activeIndex);
    },

    //切换tab 加载相应tab绑定的类别数据
    changeTab: function (tabIndex, action) {
        switch (parseInt(tabIndex)) {
            case 0:
                if (action === 'pull') {
                    this.setData({
                        siteHomeIndex: 1,
                        blog_articles: []
                    });
                }
                postType = 'sitehome'; //首页博文列表
                this.loadMoreBlogPosts(this, this.data.siteHomeIndex, postType);
                break;
            case 1:
                if (action === 'pull') {
                    this.setData({
                        pickedIndex: 1,
                        blog_pickeds: []
                    });
                }
                postType = 'picked'; //精华区博文列表
                this.loadMoreBlogPosts(this, this.data.pickedIndex, postType);
                break;
            case 2:
                if (action === 'pull') {
                    this.setData({
                        recommendedIndex: 1,
                        news_recommendeds: []
                    });
                }
                newsType = 'recommended';//推荐新闻
                this.loadMoreNews(this, this.data.recommendedIndex, newsType);
                break;
            case 3:
                if (action === 'pull') {
                    this.setData({
                        hotIndex: 1,
                        news_hots: []
                    });
                }
                newsType = 'hot';//热门新闻
                this.loadMoreNews(this, this.data.hotIndex, newsType);
                break;
            case 4:
                if (action === 'pull') {
                    this.setData({
                        hotWeekIndex: 1,
                        news_hots_week: []
                    });
                }
                newsType = 'hot-week';//本周热门新闻
                this.loadMoreNews(this, this.data.hotWeekIndex, newsType);
                break;
            default:
                console.log(this.data.siteHomeIndex, 'siteHomeIndex');
                console.log(this.data.pickedIndex, 'pickedIndex');
                console.log(this.data.recommendedIndex, 'recommendedIndex');
                console.log(this.data.hotIndex, 'hotIndex');
                console.log(this.data.hotWeekIndex, 'hotWeekIndex');
                break;
        }
    },

    /**
     * 加载博文列表
     * @param object that 当前调用对象
     * @param int pageIndex
     * @param int postType 加载博文类型
     */
    loadMoreBlogPosts: function (that, pageIndex, postType) {
        wx.showLoading({
            title: '加载中',
        })

        wx.request({
            url: 'https://api.cnblogs.com/api/blogposts/@' + postType + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize,
            method: 'GET',
            header: {
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": "Bearer " + getApp().accessToken
            },
            success: function (res) {
                if (res.data.length > 0) {
                    pageIndex++;
                    if (postType === 'sitehome') {
                        that.setData({
                            blog_articles: that.data.blog_articles.concat(res.data),
                            siteHomeIndex: pageIndex
                        });

                    } else if (postType === 'picked') {
                        that.setData({
                            blog_pickeds: that.data.blog_pickeds.concat(res.data),
                            pickedIndex: pageIndex
                        });

                    }
                }
            },
            fail: function () {
                // fail
            },
            complete: function () {
                wx.hideNavigationBarLoading();
                wx.stopPullDownRefresh();
                wx.hideLoading();
            }
        });
    },

    /**
     * 加载新闻列表
     * @param object that 当前调用对象
     * @param int pageIndex 当前第几页
     * @param int newsType 加载新闻类型
     */
    loadMoreNews: function (that, pageIndex, newsType) {
        wx.showLoading({
            title: '加载中',
        })

        var url = '';
        if (newsType === 'recommended' || newsType === 'hot-week') {
            url = 'https://api.cnblogs.com/api/newsitems/@' + newsType + '?pageIndex=' + pageIndex + '&pageSize=' + pageSize;
        } else {
            url = 'https://api.cnblogs.com/api/newsitems/@' + newsType + '?startDate=2017-03-24&endDate=2017-03-25&pageIndex=' + pageIndex + '&pageSize=' + pageSize;
        }

        wx.request({
            url: url,
            method: 'GET',
            header: {
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": "Bearer " + getApp().accessToken
            },
            success: function (res) {
                if (res.data.length > 0) {
                    pageIndex++;
                    if (newsType === 'recommended') {
                        that.setData({
                            news_recommendeds: that.data.news_recommendeds.concat(res.data),
                            recommendedIndex: pageIndex
                        });
                    } else if (newsType === 'hot') {
                        that.setData({
                            news_hots: that.data.news_hots.concat(res.data),
                            hotIndex: pageIndex
                        });
                    } else if (newsType === 'hot-week') {
                        that.setData({
                            news_hots_week: that.data.news_hots_week.concat(res.data),
                            hotWeekIndex: pageIndex
                        });
                    }
                }
            },
            fail: function () {
                // fail
            },
            complete: function () {
                wx.hideNavigationBarLoading();
                wx.stopPullDownRefresh();
                wx.hideLoading();
            }
        });
    },
})
